<?php
class Intro extends MY_Controller {
	function Intro() {
		parent::__construct();
		$this->load->library('session');
		@session_start();
	}	
	
	function index () {
		$data['title'] = "Intro";	
		$data['content'] = 'front_page/content/intro';
		$check_submit = $this->input->post('username');
		
		//lay ra link lien ket HBA
		$this->db->where('order < 6');
		$this->db->where('status = 1');
		$this->db->order_by('order','ASC');
		$data['record_5'] = $this->db->get('link_hba')->result_array();
		
		$this->db->where('order > 5');
		$this->db->where('status = 1');
		$this->db->order_by('order','ASC');
		$data['record_more'] = $this->db->get('link_hba')->result_array();
		
		//lay ra nhom nguoi dung
		$data['group_user'] = $this->db->get('group_user')->result_array();
		
		//check submit
		$check_submit =  $this->input->post('check_submit');
		
		//cap cha
		$data['captcha'] = $this->get_random_color();
			
		if(!empty($check_submit)){
			$user_name =  $this->input->post('user_name');
			$full_name =  $this->input->post('full_name');
			$email     =  $this->input->post('email');	
			$password  =  $this->input->post('password');
			$gender    =  $this->input->post('gender');
			$group_user_id = $this->input->post('group_user_id');
			$date      =  $this->input->post('date');
			$month     =  $this->input->post('month');
			$year      =  $this->input->post('year');
			$date_sn      =  $year.'-'.$month.'-'.$date;
			
			//kiem tra xem ten dang nhap co ton tai hay ko 
			$checkUser = $this->db->get_where('user',array('user_name' => $user_name))->row_array();
			$checkEmail = $this->db->get_where('user',array('email' => $email))->row_array();
			
			if(count($checkUser)> 0) {
				$data['user_name'] = 'user_name';
				
				$_SESSION['user_info']['user_name'] = $user_name;
				$_SESSION['user_info']['full_name'] = $full_name;
				$_SESSION['user_info']['email']     = $email;
			} else if(count($checkEmail)>0) {
				$data['email'] = 'email';
				
				$_SESSION['user_info']['user_name'] = $user_name;
				$_SESSION['user_info']['full_name'] = $full_name;
				$_SESSION['user_info']['email']     = $email;
			} else {
				if(!empty($full_name) && !empty($user_name) && !empty($email) && !empty($password) && !empty($gender) && !empty($group_user_id)) {
					$data_value = array (
						'full_name'       => $full_name,
						'user_name'       => $user_name,
						'email'           => $email,
						'password'        => md5($password),	
						'gender'          => $gender,	
						'group_user_id'   => $group_user_id,
						'date'            => $date_sn
					);
					
					unset($_SESSION['user_info']);
					$data['success'] = "success";
					$this->mod_lib->insert('user',$data_value);
				}
			}	
		}
		
		$this->load->view('front_page/template_intro',$data);
	}	
	
	function gen_image ($img_cm) {
		$img=ImageCreate(75,25);                       // quy dinh kich co cua anh
		$bgcolor=ImageColorAllocate($img,255,255,255); // cho mau nen là mau trang
		$red=ImageColorAllocate($img,5,5,5);         // quy dinh mau cho anh	
		ImageString($img,10,10,10, $img_cm ,$red); // add cái text lay duoc tu request vào obj image
		
		// dua ra image vua tao
		header ("Content-type: image/png");
		
		//ImageSetPixel($img,0,0,$red);   // thiet lap vi tri cua diem anh
		ImagePNG($img);                   // dua ra mot anh PNG
		ImageDestroy($img);               // xoa bo nho dem luu tru $img 
	}
	
	function get_random_color(){
		$c='';
		for ($i = 0; $i<6; $i++){
			$c .=  dechex(rand(0,15));
		}
		return "$c";
	} 
}		 
?>